import smtplib
from email.mime.text import MIMEText
from email.header import Header
import base64


class Mail:
    def __init__(self):
        # 第三方 SMTP 服务
        self.mail_host = "smtp.qq.com"  # 填写邮箱服务器:这个是qq邮箱服务器，直接使用smtp.qq.com
        self.mail_pass = ''  # 填写在qq邮箱设置中获取的授权码
        self.sender = '@qq.com'  # 填写发送方邮箱地址
        # self.receivers = ['@qq.com']  # 填写收件人的邮箱，QQ邮箱或者其他邮箱，可多个，中间用,隔开

    def send(self, txt,receivers):
        subject = '账户找回'  # 发送的主题
        content = str(txt)  # 邮件内容
        message = MIMEText(content, 'plain', 'utf-8')
        message['From'] = Header(
            f'=?utf-8?B?{base64.b64encode("林芫笙业务".encode()).decode()}=?= <@qq.com>')  # 必须填发送者邮箱
        message['To'] = Header("python自动发送", 'utf-8')  # 邮件接收者姓名
        message['Subject'] = Header(subject, 'utf-8')
        try:
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)  # 建立smtp连接，qq邮箱必须用ssl边接，因此边接465端口
            smtpObj.login(self.sender, self.mail_pass)  # 登陆
            smtpObj.sendmail(self.sender, receivers, message.as_string())  # 发送
            smtpObj.quit()
            # print('邮件发送成功！！')
        except smtplib.SMTPException as e:
            print(e.__traceback__.tb_lineno, e)
            # print('邮件发送失败！！')
def get_email():
    mail = Mail()
    return mail

if __name__ == '__main__':
    mail = Mail()
    mail.send('这是你的验证码:5555',"3052472674@qq.com")
